# coding=utf-8

import os
from ctypes import *

from HCNetSDK import sys_platform, system_type, C_DWORD

if sys_platform == 'linux':
    load_library = cdll.LoadLibrary
    fun_ctype = CFUNCTYPE
elif sys_platform == 'windows':
    load_library = windll.LoadLibrary
    fun_ctype = WINFUNCTYPE
else:
    print("************不支持的平台**************")
    exit(0)

playM4dllpath_dict = {'windows64': os.path.dirname(__file__) + '\\lib\\' + 'PlayCtrl.dll',
                      'windows32': os.path.dirname(__file__) + '\\lib\\' + 'PlayCtrl.dll',
                      'linux64': os.path.dirname(__file__) + '/lib/libPlayCtrl.so',
                      'linux32': os.path.dirname(__file__) + '/lib/libPlayCtrl.so'}
playM4dllpath = playM4dllpath_dict[system_type]

# 尝试加载播放控制库
try:
    playctrllib = load_library(playM4dllpath)
except Exception as e:
    print(f"加载PlayCtrl库失败: {e}")
    playctrllib = None

# 定义预览参数结构体
class FRAME_INFO(Structure):
    _fields_ = [
        ('nWidth', c_long),
        ('nHeight', c_long),
        ('nStamp', c_long),
        ('nType', c_long),
        ('nFrameRate', c_long),
        ('dwFrameNum', C_DWORD)
    ]

LPFRAME_INFO = POINTER(FRAME_INFO)

# 显示回调函数
DISPLAYCBFUN = fun_ctype(None, c_long, c_char_p, c_long, c_long, c_long, c_long, c_long, c_long)
# 解码回调函数
DECCBFUNWIN = fun_ctype(None, c_long, POINTER(c_char), c_long, POINTER(FRAME_INFO), c_long, c_long)

# ========== 定义播放控制库的函数原型 ==========
if playctrllib:
    # 获取播放端口
    playctrllib.PlayM4_GetPort.argtypes = [POINTER(c_long)]
    playctrllib.PlayM4_GetPort.restype = c_bool
    
    # 打开流
    playctrllib.PlayM4_OpenStream.argtypes = [c_long, c_char_p, c_ulong, c_ulong]
    playctrllib.PlayM4_OpenStream.restype = c_bool
    
    # 设置解码回调
    playctrllib.PlayM4_SetDecCallBackEx.argtypes = [c_long, DECCBFUNWIN]
    playctrllib.PlayM4_SetDecCallBackEx.restype = c_bool
    
    # 设置解码回调扩展
    playctrllib.PlayM4_SetDecCallBackExMend.argtypes = [c_long, DECCBFUNWIN, c_void_p, c_long, c_void_p]
    playctrllib.PlayM4_SetDecCallBackExMend.restype = c_bool
    
    # 播放
    playctrllib.PlayM4_Play.argtypes = [c_long, c_void_p]
    playctrllib.PlayM4_Play.restype = c_bool
    
    # 输入数据
    playctrllib.PlayM4_InputData.argtypes = [c_long, c_char_p, c_ulong]
    playctrllib.PlayM4_InputData.restype = c_bool
    
    # 停止播放
    playctrllib.PlayM4_Stop.argtypes = [c_long]
    playctrllib.PlayM4_Stop.restype = c_bool
    
    # 关闭流
    playctrllib.PlayM4_CloseStream.argtypes = [c_long]
    playctrllib.PlayM4_CloseStream.restype = c_bool
    
    # 释放端口
    playctrllib.PlayM4_FreePort.argtypes = [c_long]
    playctrllib.PlayM4_FreePort.restype = c_bool
    
    # 获取最后错误
    playctrllib.PlayM4_GetLastError.argtypes = [c_long]
    playctrllib.PlayM4_GetLastError.restype = c_long
    
    # 转换为JPEG文件
    playctrllib.PlayM4_ConvertToJpegFile.argtypes = [c_char_p, c_ulong, c_long, c_long, c_long, c_char_p]
    playctrllib.PlayM4_ConvertToJpegFile.restype = c_bool

# ========== 导出函数 ==========
if playctrllib:
    PlayM4_GetPort = playctrllib.PlayM4_GetPort
    PlayM4_OpenStream = playctrllib.PlayM4_OpenStream
    PlayM4_SetDecCallBackEx = playctrllib.PlayM4_SetDecCallBackEx
    PlayM4_SetDecCallBackExMend = playctrllib.PlayM4_SetDecCallBackExMend
    PlayM4_Play = playctrllib.PlayM4_Play
    PlayM4_InputData = playctrllib.PlayM4_InputData
    PlayM4_Stop = playctrllib.PlayM4_Stop
    PlayM4_CloseStream = playctrllib.PlayM4_CloseStream
    PlayM4_FreePort = playctrllib.PlayM4_FreePort
    PlayM4_GetLastError = playctrllib.PlayM4_GetLastError
    PlayM4_ConvertToJpegFile = playctrllib.PlayM4_ConvertToJpegFile
else:
    # 定义空函数以防库加载失败
    def PlayM4_GetPort(port):
        return False
    
    def PlayM4_OpenStream(port, buffer, buf_size, max_buf_size):
        return False
    
    def PlayM4_SetDecCallBackEx(port, callback):
        return False
    
    def PlayM4_SetDecCallBackExMend(port, callback, user_data, reserved, reserved2):
        return False
    
    def PlayM4_Play(port, hwnd):
        return False
    
    def PlayM4_InputData(port, buffer, buf_size):
        return False
    
    def PlayM4_Stop(port):
        return False
    
    def PlayM4_CloseStream(port):
        return False
    
    def PlayM4_FreePort(port):
        return False
    
    def PlayM4_GetLastError(port):
        return -1
    
    def PlayM4_ConvertToJpegFile(buffer, buf_size, width, height, type, filename):
        return False

# ========== 导出结构体和回调类型 ==========
FRAME_INFO = FRAME_INFO
LPFRAME_INFO = LPFRAME_INFO
DISPLAYCBFUN = DISPLAYCBFUN
DECCBFUNWIN = DECCBFUNWIN